import httplib
import sys
import string
from compiler.ast import TryExcept

def ff(x):
	if x == ' ':
		return "%20"
	else:
		return x	


def pars_distance_request(req):
	data = string.split(req, '\n')
	dist = 0
	dist = False
	val = ""
	for s in data:
		if "distance" in s:
			dist=True
		if (dist) and ("value" in s): 
			val = s
			break
	val = val.split(":")
	
	if len(val) > 1:	
		return int(val[1].strip())
	else: 
		return -1		
	

cities = {}

with open(sys.argv[1]) as f:
	content = f.readlines()
	conn = httplib.HTTPConnection("maps.googleapis.com")	
	print ",%s"%",".join(map(lambda x:x.strip(),content))
	for origin in content:
		sys.stdout.write("%s"%(origin.strip()))
		for destination in content:
			
			req = "/maps/api/distancematrix/json?origins=%s&destinations=%s&language=en&sensor=false"%(origin.strip(),destination.strip())
			try:
				conn.request("GET", "".join(map(ff,req)))
				r = conn.getresponse()
				ress = r.read()			
			except:
				ress = ""
				conn = httplib.HTTPConnection("maps.googleapis.com")
			sys.stdout.write(",%d"%(pars_distance_request(ress)))
			sys.stdout.flush()
			#print "ggg"
		sys.stdout.write('\n')		
	
			

			
						
	

